﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

namespace Assets.Script
{
    class GameOverThreePanel : UIBase
    {
        public Text zuanshiText, goldText, jinduText, addZuanshiText, addGoldText, nowAddGoldText;
        public Image nowShowImg, moveImg,jiesuoShowImg;
        public Button shipinBtn, noTanText,lookShipinLingqvBtn,noLingqvBtn;
        public Animator moveAnimator;
        public GameObject jinduManShowObj;
        public List<Text> fanBeiText = new List<Text>();
        public float jindu = 0;
        public int zuanshiNum, goldNum;
        public int index=0;
        bool ismove = true;
        Color m_color = new Color();
        public override void Init()
        {
            base.Init();
            shipinBtn.onClick.AddListener(() =>
            {
                Debug.Log("观看视频增加金币");
                moveAnimator.speed = 0;
                ismove = false;
                movex = moveImg.GetComponent<RectTransform>().anchoredPosition.x;
                if (Mathf.Abs(movex) <= 58)
                {
                    goldNum *= 4;
                    RefureShowGoldNum(goldNum);
                }
                else if (Mathf.Abs(movex) > 58 && Mathf.Abs(movex) <= 165)
                {
                    goldNum *= 3;
                    RefureShowGoldNum(goldNum);
                }
                else if (Mathf.Abs(movex) > 165)
                {
                    goldNum *= 2;
                    RefureShowGoldNum(goldNum);
                }
                MessageCenter.Ins.BuredMess(MessId.RefureCurrencyData, new CurrencyData()
                {
                    gold = goldNum,
                    zuanshi = zuanshiNum
                });
            });
            noTanText.onClick.AddListener(() =>
            {
                CloseMePanel();
            });
            lookShipinLingqvBtn.onClick.AddListener(() =>
            {
                Debug.Log("观看视频领取皮肤");
            });
            noLingqvBtn.onClick.AddListener(() =>
            {
                jinduManShowObj.SetActive(false);
                CloseMePanel();
            });
            MessageCenter.Ins.ListenMess<CurrencyData>(MessId.RefureCurrencyDataText, RefureCurrencyDataText);
            MainPanelModel main_models = ModelManager.Ins.GetModel(MainPanelModel.m_name) as MainPanelModel;
            MessageCenter.Ins.BuredMess(MessId.RefureCurrencyDataText, main_models.GetData());
            m_color = fanBeiText[0].color;
        }
        public override void Open()
        {
            base.Open();
            float nowjindu = jindu;//进度跳动逻辑
            nowShowImg.sprite = Resources.Load<Sprite>("thumbnail/"+GameDataSprite.Ins.jiesuoSkinDatas[index].name);
            jindu += 33.5f;
            StartTween(nowjindu, jindu, 2f);
            zuanshiNum = UnityEngine.Random.Range(0, 10) > 4 ? 1 : 0;
            addZuanshiText.text = zuanshiNum.ToString();
            goldNum = PlayerPrefs.GetInt("PlayerTH") * PlayerPrefs.GetInt("PlayerLevel") * 100;
            addGoldText.text= ShowCurrencyNum(goldNum);
            RefureShowGoldNum(goldNum);
            moveAnimator.SetBool("IsMove", true);
            moveAnimator.speed = 1;
            ismove = true;
        }

        private void RefureShowGoldNum(int num)
        {
            nowAddGoldText.text = ShowCurrencyNum(num);
            addGoldText.text = ShowCurrencyNum(num);
        }

        public void StartTween(float start, float targetValue,float duration)
        {
            StartCoroutine(TweenNumber(start, targetValue, duration));
        }
        IEnumerator TweenNumber(float startValue, float endValue, float time)
        {
            float elapsedTime = 0f;
            while (elapsedTime < time)
            {
                // 计算当前时间进度（0~1）
                float t = elapsedTime / time;
                // 插值计算当前数值
                float currentValue = Mathf.Lerp(startValue, endValue, t);
                // 更新文本（保留一位小数）
                jinduText.text = currentValue.ToString("F1");
                nowShowImg.fillAmount = currentValue / 100f;
                // 更新已用时间
                elapsedTime += Time.deltaTime;
                yield return null;
            }
            // 确保最终值准确
            if (endValue >= 100) 
            {
                jinduManShowObj.SetActive(true);
                jiesuoShowImg.sprite = Resources.Load<Sprite>("thumbnail/" + GameDataSprite.Ins.jiesuoSkinDatas[index].name);
                jindu = 0;
                index++;
                endValue = 100;
            } 
            jinduText.text = endValue.ToString("F1");
        }
        private void RefureCurrencyDataText(CurrencyData data)
        {
            zuanshiText.text = ShowCurrencyNum(data.zuanshi);
            goldText.text = ShowCurrencyNum(data.gold);
        }
        private string ShowCurrencyNum(int gold)
        {
            if (gold < 0)
                return "-" + ShowCurrencyNum(-gold);

            if (gold < 1000)
            {
                return gold.ToString();
            }
            else if (gold < 1_000_000)
            {
                return (gold / 1000.0).ToString("0.#") + "k";
            }
            else
            {
                return (gold / 1_000_000.0).ToString("0.#") + "m";
            }
        }
        void CloseMePanel()//这边是第一个关闭代码
        {
            UIManager.Ins.CloseUI(UIName.GameOverThreePanel);
            moveAnimator.SetBool("IsMove", false);
            SceneManager.LoadScene("MainScenes");
            SceneManager.sceneLoaded += (a, b) =>
            {
                if (SceneManager.GetActiveScene().name == "MainScenes")
                    UIManager.Ins.OpenUI(UIName.MainPanel);
            };
        }
        float movex = 0;
        public override void Update()
        {
            base.Update();
            if (ismove)
            {
                movex = moveImg.GetComponent<RectTransform>().anchoredPosition.x;
                if (Mathf.Abs(movex) <= 58)
                {
                    fanBeiText[0].color = m_color;
                    fanBeiText[1].color = m_color;
                    fanBeiText[2].color = Color.yellow;
                    fanBeiText[3].color = m_color;
                    fanBeiText[4].color = m_color;
                    RefureShowGoldNum(goldNum*4);
                }
                else if (Mathf.Abs(movex) > 58 && Mathf.Abs(movex) <= 165)
                {
                    if (movex > 0)
                    {
                        fanBeiText[0].color = m_color;
                        fanBeiText[1].color = m_color;
                        fanBeiText[2].color = m_color;
                        fanBeiText[3].color = Color.green;
                        fanBeiText[4].color = m_color;
                    }
                    else
                    {
                        fanBeiText[0].color = m_color;
                        fanBeiText[1].color = Color.green;
                        fanBeiText[2].color = m_color;
                        fanBeiText[3].color = m_color;
                        fanBeiText[4].color = m_color;
                    }

                    RefureShowGoldNum(goldNum*3);
                } 
                else if (Mathf.Abs(movex) > 165)
                {
                    if (movex > 0)
                    {
                        fanBeiText[0].color = m_color;
                        fanBeiText[1].color = m_color;
                        fanBeiText[2].color = m_color;
                        fanBeiText[3].color = m_color;
                        fanBeiText[4].color = Color.white;
                    }
                    else
                    {
                        fanBeiText[0].color = Color.white;
                        fanBeiText[1].color = m_color;
                        fanBeiText[2].color = m_color;
                        fanBeiText[3].color = m_color;
                        fanBeiText[4].color = m_color;
                    }
                    RefureShowGoldNum(goldNum*2);
                }
            }
        }
    }
}
